import Cookies from "js-cookie";

const TokenKey = "Admin-Token";
const UserIdKey = "userId";
const CacheType = "localStorage";
const UserInfoKey = "userInfo";

const AuthCache = {
  set: (key, value) => {
    if (CacheType === "localStorage") {
      return localStorage.setItem(key, value);
    }
    if (CacheType === "sessionStorage") {
      return sessionStorage.setItem(key, value);
    }
    if (CacheType === "cookie") {
      return Cookies.set(key, value);
    }
  },
  get: key => {
    if (CacheType === "localStorage") {
      return localStorage.getItem(key);
    }
    if (CacheType === "sessionStorage") {
      return sessionStorage.getItem(key);
    }
    if (CacheType === "cookie") {
      return Cookies.get(key);
    }
  },
  remove: key => {
    if (CacheType === "localStorage") {
      return localStorage.removeItem(key);
    }
    if (CacheType === "sessionStorage") {
      return sessionStorage.removeItem(key);
    }
    if (CacheType === "cookie") {
      return Cookies.remove(key);
    }
  },
};

export function getToken() {
  return AuthCache.get(TokenKey);
}

export function setToken(token) {
  return AuthCache.set(TokenKey, token);
}

export function removeToken() {
  return AuthCache.remove(TokenKey);
}

export function getUserId() {
  return AuthCache.get(UserIdKey);
}

export function setUserId(userId) {
  return AuthCache.set(UserIdKey, userId);
}

export function removeUserId() {
  return AuthCache.remove(UserIdKey);
}

export function getUserInfo() {
  return JSON.parse(AuthCache.get(UserInfoKey));
}

export function setUserInfo(userInfo) {
  return AuthCache.set(UserInfoKey, JSON.stringify(userInfo));
}

export function removeUserInfo() {
  return AuthCache.remove(UserInfoKey);
}
